/*
 * @Author: Yhd you@example.com
 * @Date: 2023-02-07 22:25:56
 * @LastEditors: yan_hai_dong 891236760@qq.com
 * @LastEditTime: 2023-03-14 11:05:41
 * @FilePath: \get-comment\on.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
let falg = false;
let base_url = 'https://3tg0910673.yicp.fun';
function info() {
    try {
        let img = getVideoToBase64Img()
        requer('/test/img', {
            method: 'POST',
            data: {
                img: '',
                url: img
            }
        }).then(res => {
            let {
                code,
                data
            } = res;
            if (code == 200) {
                let {
                    ImageWidth,
                    ImageHeight,
                    FaceInfos: [{
                        X,
                        Y,
                        Width,
                        Height,
                        FaceAttributesInfo: {
                            Gender,
                            Age,
                            Expression,
                            Glass,
                            Pitch,
                            Yaw,
                            Roll,
                            Beauty,
                            Hat,
                            Mask,
                            Hair: {
                                Bang,
                                Color,
                                Length,
                            },
                            EyeOpen
                        },
                        FaceQualityInfo: {
                            Score,
                            Sharpness,
                            Brightness,
                            Completeness: {
                                Eyebrow,
                                Eye,
                                Nose,
                                Cheek,
                                Mouth,
                                Chin,
                            }
                        },
                    }],
                    FaceModelVersion,
                } = data
                // console.log({
                //     Gender,
                //     Beauty
                // })
                if (Beauty >= 70) {
                // if (Gender < 50 && Beauty >= 60) {
                    getActiveVideo().querySelector(' .H2HjqV3h').click();
                    getActiveVideo().querySelector(' .HNBvVrcV').click();
                    return next('这个可以 关注了')
                }
            }
            next()
        }).catch(err => {
            next()
        })
    } catch (error) {
        console.log(error)
        next()
    }
}

function next(msg = '这个不太行 下一个') {
    console.log(`~~${msg}`)
    if (!falg) return;
    setTimeout(() => {
        keydown('down')
        setTimeout(() => {
            info();
        }, 800)
    }, 1500)
}
function keydown(key){
    const keys = {
        down: 40,
        G: 71,
        Z: 90,
    }
    let code = keys[key]
    var e = new KeyboardEvent('keydown', {
        'keyCode': code,
        'which': code
    });
    document.dispatchEvent(e)
}
function getVideoToBase64Img() {
    const video = getActiveVideo().querySelector('.OaNxZqFU img')
    return video.src
}
function getActiveVideo() {
    // const video = document.querySelector('.swiper-slide.swiper-slide-active')
    return document.querySelector('[data-e2e="feed-active-video"]')
}
function requer(url, {
    method = 'GET',

    data = {}
}) {
    return fetch(`${base_url}${url}`, {
        method,
        body: JSON.stringify(data),
        cors: 'no cors',
        headers: {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        },
    }).then((res) => {
        if (res.status === 200) {
            return res.json()
        } else return {}
    })
}

chrome.runtime.onMessage.addListener((message) => {
    if (message.extension == 'ON') {
        falg = true;
        info();
    } else {
        falg = false
        console.log('关闭');
    }
})